package cn.jimmie.learn.viewArt.fragment

import android.support.v7.app.AlertDialog
import android.view.View
import cn.jimmie.learn.viewArt.R
import cn.jimmie.learn.viewArt.weight.ButterflyAnimatorView
import kotlinx.android.synthetic.main.fragment_game.view.*

/**
 * function :
 *
 * <p> Create Time:  2019年01月17日 13:01 </p>
 * <p> @author Jimmie.Qian  </p>
 */

class GameFragment : BaseFragment() {

    private var score = 0

    override fun initView(root: View) {
        root.up.setOnClickListener {
            root.my_view.go(ButterflyAnimatorView.TOP)
        }
        root.down.setOnClickListener {
            root.my_view.go(ButterflyAnimatorView.BOTTOM)
        }
        root.l.setOnClickListener {
            root.my_view.go(ButterflyAnimatorView.LEFT)
        }
        root.r.setOnClickListener {
            root.my_view.go(ButterflyAnimatorView.RIGHT)
        }

        val dialog = AlertDialog.Builder(context!!)
            .setTitle("GAME OVER!")
            .setNegativeButton("QUIT") { _, _ ->
                activity!!.finish()
            }
            .setPositiveButton("OK") { it, _ ->
                it.dismiss()
                score = 0
                root.score.text = "$score"
                root.my_view.reset()
            }
            .create()

        root.my_view.mListener = object : ButterflyAnimatorView.OnEventListener {
            override fun onHitWall() {
                dialog.setMessage("Your SCORE is $score! Click 'OK' to continue!")
                dialog.show()
            }

            override fun onHitFood() {
                score += 1
                root.score.text = "$score"
            }
        }
    }

    override fun layoutId(): Int = R.layout.fragment_game
}